<?php

namespace App\HttpController\Api\Attend\Student;

use App\HttpController\Api\Admin\AdminBase;
use App\Lib\Utils;
use App\Model\Attend\Leave as LeaveModel;

class Leave extends AdminBase
{

    /**
     * 请假类型列表
     */
    public function index()
    {
        $where = [
            'scope' => 1,
            'school_code' => $this->userinfo['school_code']
        ];

        if (isset($this->params['name']) && !empty($this->params['name'])) {
            $where['name'] = ['%'.$this->params['name'].'%','like'];
        }

        try {
            $field = '`id`, `name`, `create_time`, `remark`';
            $leaveObj = new LeaveModel();
            $data = $leaveObj->getPageList($field, $where, $this->params['page'], $this->params['limit']);

        } catch(\Exception $e) {
            return $this->errorJson("Server Error");
        }

        if (!empty($data['lists'])) {
            foreach($data['lists'] as &$list) {
                $list['create_time'] = date("Y-m-d H:i:s", $list['create_time']);
            }
        }

        $this->dataJson([
            'data' => $data['lists'],
            'count' => $data['count']
        ]);
    }

    /**
     * 添加请假类型
     */
    public function add()
    {
        $str = $this->raw;
        $this->params = json_decode(Utils::characet($str), true);

        $data['name'] = $this->params['name'];
        $data['remark'] = $this->params['remark'];

        try {
            $leaveObj = new LeaveModel();

            $where = [];
            $where['scope'] = 1; // 学生
            $where['school_code'] = $this->userinfo['school_code'];
            $where['name'] = $data['name'];
            $isset = $leaveObj->getOne('id', $where);

            if ($isset && is_array($isset)) {
                return $this->errorJson('假期已存在');
            }

            $data['scope'] = 1;
            $data['user_id'] = $this->userinfo['user_id'];
            $data['school_code'] = $this->userinfo['school_code'];
            $data['create_time'] = time();
            $data['status'] = 1;

            $id = $leaveObj->add($data);

        } catch(\Exception $e) {
            return $this->errorJson("Server Error");
        }

        if (!empty($id)) {
            return $this->successJson('添加成功');
        } else {
            return $this->errorJson('操作数据失败');
        }
    }

    /**
     * 编辑请假类型
     */
    public function edit()
    {
        $str = $this->raw;
        $this->params = json_decode(Utils::characet($str), true);

        if (!isset($this->params['id']) || empty($this->params['id'])) {
            return $this->errorJson('提交数据有误');
        }
        $data['name'] = $this->params['name'];
        $data['remark'] = $this->params['remark'];

        $id = $this->params['id'];
        try {
            $leaveObj = new LeaveModel();

            $where = [];
            $where['scope'] = 1; // 学生
            $where['school_code'] = $this->userinfo['school_code'];
            $where['name'] = $data['name'];
            $where['id'] = [$id, '<>'];

            $isset = $leaveObj->getOne('id', $where);

            if ($isset && is_array($isset)) {
                return $this->errorJson('名称已存在');
            }
            $data['update_time'] = time();
            $result = $leaveObj->update($data, ['id' => $id]);

        } catch(\Exception $e) {
            return $this->errorJson("Server Error");
        }

        if ($result) {
            return $this->successJson('修改成功');
        } else {
            return $this->errorJson('操作数据失败');
        }
    }

    /**
     * 删除类型
     */
    public function delete()
    {
        $str = $this->raw;
        $this->params = json_decode(Utils::characet($str), true);

        if (isset($this->params['ids']) && !empty($this->params['ids'])) {
            $ids = $this->params['ids'];
        } else {
            return $this->errorJson('删除数据失败');
        }

        try {
            $leaveObj = new LeaveModel();
            $result = $leaveObj->destroy($ids);
        } catch(\Exception $e) {
            return $this->errorJson("Server Error");
        }

        if ($result) {
            return $this->successJson('删除成功');
        } else {
            return $this->errorJson('删除失败');
        }
    }

    /**
     * 获取类型简单列表
     */
    public function select()
    {
        try {
            $leaveObj = new LeaveModel();
            $result = $leaveObj->getSelData([
                'scope' => 1,
                'school_code' => $this->userinfo['school_code']
            ]);
        } catch(\Exception $e) {
            echo $e->getMessage();
            return $this->errorJson('Server Error');
        }

        if ($result) {
            return $this->dataJson($result);
        } else {
            return $this->errorJson('FAIL');
        }
    }

}